﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading.Tasks;

namespace ebMS3dotnet.Core
{
    class ebMS3PackagingChannelFactory<TShape>:ChannelFactoryBase<TShape>
    {
        private string _pMode;

        public string PMode
        {
            get { return _pMode; }
            set { _pMode = value; }
        }
        IChannelFactory<TShape> _innerFactory;

        internal ebMS3PackagingChannelFactory(BindingContext context)
        {
            _innerFactory = context.BuildInnerChannelFactory<TShape>();
        }

        protected override TShape OnCreateChannel(System.ServiceModel.EndpointAddress address, Uri via)
        {
            TShape innerChannel = _innerFactory.CreateChannel(address, via);

            if (typeof(TShape) == typeof(IRequestChannel))
                return (TShape)(object)new ebMS3PackagingRequestChannel(this, (IRequestChannel)innerChannel);

            throw new ArgumentException(string.Format("Invalid channel shape: {0}", innerChannel.GetType()));
        }

        protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
        {
            return _innerFactory.BeginOpen(timeout, callback, state);
        }

        protected override void OnEndOpen(IAsyncResult result)
        {
            _innerFactory.EndOpen(result);
        }

        protected override void OnOpen(TimeSpan timeout)
        {
            _innerFactory.Open(timeout);
        }
    }
}
